import { request } from '../utils/request'

// 系统设置API

/**
 * 获取系统设置
 * @returns {Promise}
 */
export const getSystemSettings = () => {
  return request.get('/settings/system')
}

/**
 * 更新系统设置
 * @param {Object} settings - 系统设置
 * @returns {Promise}
 */
export const updateSystemSettings = settings => {
  return request.put('/settings/system', settings, {
    loadingText: '正在更新系统设置...'
  })
}

/**
 * 获取ONVIF设置
 * @returns {Promise}
 */
export const getOnvifSettings = () => {
  return request.get('/settings/onvif')
}

/**
 * 更新ONVIF设置
 * @param {Object} settings - ONVIF设置
 * @returns {Promise}
 */
export const updateOnvifSettings = settings => {
  return request.put('/settings/onvif', settings, {
    loadingText: '正在更新ONVIF设置...'
  })
}

/**
 * 获取流媒体设置
 * @returns {Promise}
 */
export const getStreamingSettings = () => {
  return request.get('/settings/streaming')
}

/**
 * 更新流媒体设置
 * @param {Object} settings - 流媒体设置
 * @returns {Promise}
 */
export const updateStreamingSettings = settings => {
  return request.put('/settings/streaming', settings, {
    loadingText: '正在更新流媒体设置...'
  })
}

/**
 * 获取网络设置
 * @returns {Promise}
 */
export const getNetworkSettings = () => {
  return request.get('/settings/network')
}

/**
 * 更新网络设置
 * @param {Object} settings - 网络设置
 * @returns {Promise}
 */
export const updateNetworkSettings = settings => {
  return request.put('/settings/network', settings, {
    loadingText: '正在更新网络设置...'
  })
}

/**
 * 获取存储设置
 * @returns {Promise}
 */
export const getStorageSettings = () => {
  return request.get('/settings/storage')
}

/**
 * 更新存储设置
 * @param {Object} settings - 存储设置
 * @returns {Promise}
 */
export const updateStorageSettings = settings => {
  return request.put('/settings/storage', settings, {
    loadingText: '正在更新存储设置...'
  })
}

/**
 * 获取日志设置
 * @returns {Promise}
 */
export const getLogSettings = () => {
  return request.get('/settings/log')
}

/**
 * 更新日志设置
 * @param {Object} settings - 日志设置
 * @returns {Promise}
 */
export const updateLogSettings = settings => {
  return request.put('/settings/log', settings, {
    loadingText: '正在更新日志设置...'
  })
}

/**
 * 获取安全设置
 * @returns {Promise}
 */
export const getSecuritySettings = () => {
  return request.get('/settings/security')
}

/**
 * 更新安全设置
 * @param {Object} settings - 安全设置
 * @returns {Promise}
 */
export const updateSecuritySettings = settings => {
  return request.put('/settings/security', settings, {
    loadingText: '正在更新安全设置...'
  })
}

/**
 * 获取通知设置
 * @returns {Promise}
 */
export const getNotificationSettings = () => {
  return request.get('/settings/notification')
}

/**
 * 更新通知设置
 * @param {Object} settings - 通知设置
 * @returns {Promise}
 */
export const updateNotificationSettings = settings => {
  return request.put('/settings/notification', settings, {
    loadingText: '正在更新通知设置...'
  })
}

/**
 * 测试通知设置
 * @param {Object} settings - 通知设置
 * @returns {Promise}
 */
export const testNotificationSettings = settings => {
  return request.post('/settings/notification/test', settings, {
    loadingText: '正在测试通知设置...'
  })
}

/**
 * 获取备份设置
 * @returns {Promise}
 */
export const getBackupSettings = () => {
  return request.get('/settings/backup')
}

/**
 * 更新备份设置
 * @param {Object} settings - 备份设置
 * @returns {Promise}
 */
export const updateBackupSettings = settings => {
  return request.put('/settings/backup', settings, {
    loadingText: '正在更新备份设置...'
  })
}

/**
 * 创建备份
 * @param {Object} params - 备份参数
 * @returns {Promise}
 */
export const createBackup = (params = {}) => {
  return request.post('/settings/backup/create', params, {
    loadingText: '正在创建备份...',
    timeout: 300000 // 5分钟超时
  })
}

/**
 * 获取备份列表
 * @returns {Promise}
 */
export const getBackupList = () => {
  return request.get('/settings/backup/list')
}

/**
 * 恢复备份
 * @param {string} backupId - 备份ID
 * @returns {Promise}
 */
export const restoreBackup = backupId => {
  return request.post(
    `/settings/backup/restore/${backupId}`,
    {},
    {
      loadingText: '正在恢复备份...',
      timeout: 300000 // 5分钟超时
    }
  )
}

/**
 * 删除备份
 * @param {string} backupId - 备份ID
 * @returns {Promise}
 */
export const deleteBackup = backupId => {
  return request.delete(`/settings/backup/${backupId}`, {
    loadingText: '正在删除备份...'
  })
}

/**
 * 下载备份
 * @param {string} backupId - 备份ID
 * @returns {Promise}
 */
export const downloadBackup = backupId => {
  return request.download(
    `/settings/backup/download/${backupId}`,
    {},
    {
      loadingText: '正在下载备份...'
    }
  )
}

/**
 * 上传备份
 * @param {FormData} formData - 备份文件
 * @returns {Promise}
 */
export const uploadBackup = formData => {
  return request.upload('/settings/backup/upload', formData, {
    loadingText: '正在上传备份...'
  })
}

/**
 * 重置设置
 * @param {string} category - 设置类别
 * @returns {Promise}
 */
export const resetSettings = category => {
  return request.post(
    `/settings/reset/${category}`,
    {},
    {
      loadingText: '正在重置设置...'
    }
  )
}

/**
 * 导出设置
 * @param {string[]} categories - 设置类别列表
 * @returns {Promise}
 */
export const exportSettings = (categories = []) => {
  return request.download(
    '/settings/export',
    { categories },
    {
      loadingText: '正在导出设置...'
    }
  )
}

/**
 * 导入设置
 * @param {FormData} formData - 设置文件
 * @returns {Promise}
 */
export const importSettings = formData => {
  return request.upload('/settings/import', formData, {
    loadingText: '正在导入设置...'
  })
}

/**
 * 获取系统信息
 * @returns {Promise}
 */
export const getSystemInfo = () => {
  return request.get(
    '/settings/system/info',
    {},
    {
      loading: false
    }
  )
}

/**
 * 获取系统状态
 * @returns {Promise}
 */
export const getSystemStatus = () => {
  return request.get(
    '/settings/system/status',
    {},
    {
      loading: false
    }
  )
}

/**
 * 获取系统资源使用情况
 * @returns {Promise}
 */
export const getSystemResources = () => {
  return request.get(
    '/settings/system/resources',
    {},
    {
      loading: false
    }
  )
}

/**
 * 重启系统
 * @returns {Promise}
 */
export const restartSystem = () => {
  return request.post(
    '/settings/system/restart',
    {},
    {
      loadingText: '正在重启系统...'
    }
  )
}

/**
 * 关闭系统
 * @returns {Promise}
 */
export const shutdownSystem = () => {
  return request.post(
    '/settings/system/shutdown',
    {},
    {
      loadingText: '正在关闭系统...'
    }
  )
}

/**
 * 清理系统缓存
 * @returns {Promise}
 */
export const clearSystemCache = () => {
  return request.post(
    '/settings/system/clear-cache',
    {},
    {
      loadingText: '正在清理缓存...'
    }
  )
}

/**
 * 清理系统日志
 * @returns {Promise}
 */
export const clearSystemLogs = () => {
  return request.post(
    '/settings/system/clear-logs',
    {},
    {
      loadingText: '正在清理日志...'
    }
  )
}

/**
 * 获取许可证信息
 * @returns {Promise}
 */
export const getLicenseInfo = () => {
  return request.get('/settings/license')
}

/**
 * 更新许可证
 * @param {string} licenseKey - 许可证密钥
 * @returns {Promise}
 */
export const updateLicense = licenseKey => {
  return request.put(
    '/settings/license',
    { licenseKey },
    {
      loadingText: '正在更新许可证...'
    }
  )
}

/**
 * 验证许可证
 * @returns {Promise}
 */
export const validateLicense = () => {
  return request.post(
    '/settings/license/validate',
    {},
    {
      loadingText: '正在验证许可证...'
    }
  )
}

/**
 * 获取更新信息
 * @returns {Promise}
 */
export const getUpdateInfo = () => {
  return request.get('/settings/update/info')
}

/**
 * 检查更新
 * @returns {Promise}
 */
export const checkUpdate = () => {
  return request.post(
    '/settings/update/check',
    {},
    {
      loadingText: '正在检查更新...'
    }
  )
}

/**
 * 下载更新
 * @returns {Promise}
 */
export const downloadUpdate = () => {
  return request.post(
    '/settings/update/download',
    {},
    {
      loadingText: '正在下载更新...',
      timeout: 600000 // 10分钟超时
    }
  )
}

/**
 * 安装更新
 * @returns {Promise}
 */
export const installUpdate = () => {
  return request.post(
    '/settings/update/install',
    {},
    {
      loadingText: '正在安装更新...',
      timeout: 600000 // 10分钟超时
    }
  )
}

/**
 * 获取更新历史
 * @returns {Promise}
 */
export const getUpdateHistory = () => {
  return request.get('/settings/update/history')
}

/**
 * 获取配置模板
 * @returns {Promise}
 */
export const getConfigTemplates = () => {
  return request.get('/settings/templates')
}

/**
 * 应用配置模板
 * @param {string} templateId - 模板ID
 * @returns {Promise}
 */
export const applyConfigTemplate = templateId => {
  return request.post(
    `/settings/templates/${templateId}/apply`,
    {},
    {
      loadingText: '正在应用配置模板...'
    }
  )
}

/**
 * 创建配置模板
 * @param {Object} template - 配置模板
 * @returns {Promise}
 */
export const createConfigTemplate = template => {
  return request.post('/settings/templates', template, {
    loadingText: '正在创建配置模板...'
  })
}

/**
 * 更新配置模板
 * @param {string} templateId - 模板ID
 * @param {Object} template - 配置模板
 * @returns {Promise}
 */
export const updateConfigTemplate = (templateId, template) => {
  return request.put(`/settings/templates/${templateId}`, template, {
    loadingText: '正在更新配置模板...'
  })
}

/**
 * 删除配置模板
 * @param {string} templateId - 模板ID
 * @returns {Promise}
 */
export const deleteConfigTemplate = templateId => {
  return request.delete(`/settings/templates/${templateId}`, {
    loadingText: '正在删除配置模板...'
  })
}
